using System;
using System.Data;
using THSERP.BusinessEntities;
using THSERP.BaseProvider;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using THSLib;
using System.Windows.Forms;


namespace THSERP.Modules.Report
{
    #region ReportModule
    public class ReportModule : BaseModuleERP
    {
        #region Constants
        public const String cstReportTypeSale = "Sale";
        public const String cstReportTypeCustomer = "Customer";
        public const String cstReportTypeInventory = "Inventory";
        public const String cstReportTypePurchasing = "Purchasing";
        public const String cstReportTypeEmployee = "Employee";

        public const String cstReportSectionGeneral = "General";
        public const String cstReportSectionGraph = "Graph";
        public const String cstReportSectionMultiStore = "Multi-store";
        #endregion

        public ReportModule()
        {
            Name = "Report";
            InitializeModule();
        }

        public override void InitializeScreens()
        {
            guiReportCenter guiReportCenter = new guiReportCenter();
            guiReportCenter.ScreenNumber = "DMRP100";
            guiReportCenter.Module = this;
            Screens.Add(guiReportCenter);

            guiReportCenter.AddControlsToParentScreen();
        }
        //hieu.nguyen fixed 09/08/2011
        //public String GenerateInvoiceItemSearchQuery(DateTime from, DateTime to, int departmentID, int productGroupID, int supplierID, int branchID, String groupBy)
        //{
        //    StringBuilder queryBuilder = new StringBuilder();
        //    queryBuilder.Append(String.Format("SELECT {0}, SUM(ii.ARInvoiceItemProductQty) as ARInvoiceItemProductQty, SUM(ii.ARInvoiceItemNetAmount) as ARInvoiceItemNetAmount, SUM(ii.ARInvoiceItemTotalCost) as ARInvoiceItemTotalCost", groupBy));
        //    queryBuilder.Append(THSUtil.NewLine + "FROM ARInvoiceItems ii");
        //    queryBuilder.Append(THSUtil.NewLine + "INNER JOIN ARInvoices i ON ii.FK_ARInvoiceID = i.ARInvoiceID");
        //    queryBuilder.Append(THSUtil.NewLine + "INNER JOIN ICProducts p ON ii.FK_ICProductID = p.ICProductID");
        //    queryBuilder.Append(THSUtil.NewLine + "LEFT JOIN APSuppliers s ON p.FK_APSupplierID = s.APSupplierID");
        //    queryBuilder.Append(THSUtil.NewLine + "INNER JOIN ICProductGroups pg ON p.FK_ICProductGroupID = pg.ICProductGroupID");
        //    queryBuilder.Append(THSUtil.NewLine + "INNER JOIN ICDepartments d ON p.FK_ICDepartmentID = d.ICDepartmentID");
        //    queryBuilder.Append(THSUtil.NewLine + String.Format("WHERE i.AAStatus = 'Alive' AND ii.AAStatus = 'Alive' AND p.AAStatus = 'Alive'"));
        //    if (branchID > 0)
        //        queryBuilder.Append(String.Format("AND i.FK_BRBranchID = {0}", branchID));

        //    if (from.ToString("yyyyMMdd") != "00010101")
        //        queryBuilder.Append(String.Format(" AND CONVERT(VARCHAR(10), '{0}', 112) <= CONVERT(VARCHAR(10), i.ARInvoiceDate, 112)", from.ToString("yyyyMMdd")));

        //    if (to.ToString("yyyyMMdd") != "00010101")
        //        queryBuilder.Append(String.Format("AND CONVERT(VARCHAR(10), i.ARInvoiceDate, 112) <= CONVERT(VARCHAR(10), '{0}', 112)", to.ToString("yyyyMMdd")));

        //    if (departmentID > 0)
        //        queryBuilder.Append(String.Format(" AND d.AAStatus = 'Alive' AND d.ICDepartmentID = {0}", departmentID));

        //    if (productGroupID > 0)
        //        queryBuilder.Append(String.Format(" AND pg.AAStatus = 'Alive' AND pg.ICProductGroupID = {0}", productGroupID));

        //    if (supplierID > 0)
        //        queryBuilder.Append(String.Format(" AND s.AAStatus = 'Alive' AND s.APSupplierID = {0}", supplierID));

        //    queryBuilder.Append(THSUtil.NewLine + String.Format("GROUP BY {0}", groupBy));
        //    return queryBuilder.ToString();
        //}
        public String GenerateInvoiceItemSearchQuery(DateTime from, DateTime to, int departmentID, int productGroupID, int supplierID, int branchID, String groupBy)
        {
            StringBuilder queryBuilder = new StringBuilder();
            queryBuilder.Append(String.Format("SELECT {0}, SUM(ii.ARInvoiceItemProductQty) as ARInvoiceItemProductQty,SUM(ii.ARInvoiceItemNetAmount) as ARInvoiceItemNetAmount, SUM(ii.ARInvoiceItemTotalCost) as ARInvoiceItemTotalCost", groupBy));
            queryBuilder.Append(THSUtil.NewLine + "FROM ARInvoiceItems ii");
            queryBuilder.Append(THSUtil.NewLine + "INNER JOIN ARInvoices i ON ii.FK_ARInvoiceID = i.ARInvoiceID");
            queryBuilder.Append(THSUtil.NewLine + "INNER JOIN ICProducts p ON ii.FK_ICProductID = p.ICProductID");
            queryBuilder.Append(THSUtil.NewLine + "LEFT JOIN APSuppliers s ON p.FK_APSupplierID = s.APSupplierID");
            queryBuilder.Append(THSUtil.NewLine + "INNER JOIN ICProductGroups pg ON p.FK_ICProductGroupID = pg.ICProductGroupID");
            queryBuilder.Append(THSUtil.NewLine + "INNER JOIN ICDepartments d ON p.FK_ICDepartmentID = d.ICDepartmentID");
            queryBuilder.Append(THSUtil.NewLine + String.Format("WHERE i.AAStatus = 'Alive' AND ii.AAStatus = 'Alive' AND p.AAStatus = 'Alive'"));
            if (branchID > 0)
                queryBuilder.Append(String.Format("AND i.FK_BRBranchID = {0}", branchID));

            if (from.ToString("yyyyMMdd") != "00010101")
                queryBuilder.Append(String.Format(" AND CONVERT(VARCHAR(10), '{0}', 112) <= CONVERT(VARCHAR(10), i.ARInvoiceDate, 112)", from.ToString("yyyyMMdd")));

            if (to.ToString("yyyyMMdd") != "00010101")
                queryBuilder.Append(String.Format("AND CONVERT(VARCHAR(10), i.ARInvoiceDate, 112) <= CONVERT(VARCHAR(10), '{0}', 112)", to.ToString("yyyyMMdd")));

            if (departmentID > 0)
                queryBuilder.Append(String.Format(" AND d.AAStatus = 'Alive' AND d.ICDepartmentID = {0}", departmentID));

            if (productGroupID > 0)
                queryBuilder.Append(String.Format(" AND pg.AAStatus = 'Alive' AND pg.ICProductGroupID = {0}", productGroupID));

            if (supplierID > 0)
                queryBuilder.Append(String.Format(" AND s.AAStatus = 'Alive' AND s.APSupplierID = {0}", supplierID));

            queryBuilder.Append(THSUtil.NewLine + String.Format("GROUP BY {0}", groupBy));
            return queryBuilder.ToString();
        }
        //
        public void Print(DevExpress.XtraPrinting.IPrintable control)
        {
            DevExpress.XtraPrinting.PrintingSystem printingSystem = new DevExpress.XtraPrinting.PrintingSystem();
            DevExpress.XtraPrinting.PrintableComponentLink printLink = new DevExpress.XtraPrinting.PrintableComponentLink();

            try
            {
                //Set to landscape
                printingSystem.PageSettings.Landscape = true;
               // Cursor  = Cursors.WaitCursor;
                printLink.Component = control;
                printLink.CreateDocument(printingSystem);
                printLink.VerticalContentSplitting = DevExpress.XtraPrinting.VerticalContentSplitting.Exact;
                printingSystem.PreviewFormEx.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                //Cursor = Cursors.Default;
                printingSystem.Dispose();
                printLink.Dispose();
            }
        }
    }
    #endregion
}
